<template>
  <!-- 轮播图 -->
  <div class="swiper-container" ref="mySwiper">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="item in list" :key="item.id">
        <img :src="item.imgUrl" />
      </div>
    </div>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>

    <!-- 如果需要导航按钮 -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
  </div>
</template>

<script>
import Swiper, { Pagination, Navigation } from "swiper";

export default {
  name: "carousel-vue",
  props: ["list"],
  watch: {
    list: {
      immediate: true, //开始就执行一次，因为props拿来就是完整的数据，从来没有变化过，所以不写监听不到
      handler() {
        //因为floorlist在home父组件通过异步已经拿到传给了floor子组件，所以可以使用mounted，因为一开始的数据就是完整的。
        //不过因为我们需要封装，所以拿出去和watch一样写法
        this.$nextTick(function () {
          Swiper.use([Pagination, Navigation]);
          var mySwiper = new Swiper(this.$refs.mySwiper, {
            loop: true, // 循环模式选项
            pagination: {
              el: ".swiper-pagination",
              clickable: true,
            },
            // 如果需要前进后退按钮
            navigation: {
              nextEl: ".swiper-button-next",
              prevEl: ".swiper-button-prev",
            },
          });
        });
      },
    },
  },
};
</script>

<style>
</style>